package com.visec.hdy.sync.ftp.code;

import java.io.IOException;

import com.visec.hdy.sync.ftp.config.SyncFileMsg;
import com.visec.hdy.sync.ftp.config.TransmissionAction;
import com.visec.hdy.transmission.beans.FileModel;
import com.visec.hdy.transmission.beans.FileModelHandle;
import com.visec.hdy.transmission.socket.code.SocketClient;

public class FtpFileModelHandle implements FileModelHandle {
	private int a = 0;
	private Transmission transmission;
	private String ftpId;
	private SocketClient socketClient;
	private boolean isStart = false;

	public FtpFileModelHandle(Transmission transmission, String ftpId, SocketClient socketClient) {
		this.transmission = transmission;
		this.ftpId = ftpId;
		this.socketClient = socketClient;
	}

	@Override
	public boolean handleFile(FileModel fileModel) {
		// TODO Auto-generated method stub
		if (!isStart)
			return false;
		int result = transmission.fileCheckAndSync(new SyncFileMsg(fileModel, ftpId, TransmissionAction.check_sync_),
				socketClient);
		if (result >= 0) {
			a = a + result;
		} else {
			isStart = false;
			return false;
		}
		return true;
	}

	public int getSyncSize() {
		return a;
	}

	@Override
	public boolean open() {
		// TODO Auto-generated method stub
		if (socketClient == null)
			return false;
		if (!socketClient.isConnected())
			try {
				socketClient.connect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		a=0;
		isStart = true;
		return true;
	}

	@Override
	public boolean close() {
		// TODO Auto-generated method stub
		isStart = false;
		if (socketClient != null)
			try {
				socketClient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		return true;
	}

}
